Skip to content

fix: consolidate evicted entries into long-term memory#166

Open
yashhzd wants to merge 2 commits intomesa:mainfrom
yashhzd:fix/stlt-consolidation-lost-memories-107
Open

fix: consolidate evicted entries into long-term memory#166
yashhzd wants to merge 2 commits intomesa:mainfrom
yashhzd:fix/stlt-consolidation-lost-memories-107

Conversation

@yashhzd
Copy link
Contributor

@yashhzd yashhzd commented Mar 6, 2026

Summary

Fixes #107

STLTMemory._process_step_core() discards popped short-term entries without passing them to the LLM for summarization. Instead, _build_consolidation_prompt() formats the remaining active short-term memories — so the LLM redundantly summarizes memories that are still in short-term, while the evicted ones are permanently lost.

Root Cause

  1. self.short_term_memory.popleft() discards the entry (return value is ignored)
  2. Only one entry is popped even when consolidation_capacity could be > 1
  3. _build_consolidation_prompt() sends the remaining short-term memories to the LLM — the evicted entries are never seen

Changes

  • _process_step_core(): Now captures all evicted entries (up to consolidation_capacity) in a list and returns them alongside the new entry
  • _build_consolidation_prompt(evicted_entries): Accepts the evicted entries and constructs a prompt asking the LLM to integrate them into the existing long-term summary
  • _update_long_term_memory(evicted_entries) / _aupdate_long_term_memory(evicted_entries): Updated signatures to receive and forward evicted entries
  • process_step() / aprocess_step(): Pass evicted entries to the consolidation methods
  • Tests: Updated existing test_update_long_term_memory and test_long_term_memory_stores_string_not_response_object to match new signatures; added test_consolidation_receives_evicted_entries regression test

Before vs After

Before After
Evicted entries Discarded silently Captured and passed to LLM
LLM receives Remaining short-term memories Evicted entries + existing long-term summary
Entries popped per trigger Always 1 consolidation_capacity entries

Test Plan

  • All 263 existing tests pass
  • 1 new regression test for consolidation behavior
  • Pre-commit hooks pass (ruff, codespell)

STLTMemory._process_step_core() was discarding popped entries and
then summarizing the *remaining* short-term memories instead of the
evicted ones.  This caused permanent data loss — old memories were
deleted without ever being integrated into long-term memory.

Changes:
- _process_step_core now captures all evicted entries and returns
  them alongside the new entry
- Pop consolidation_capacity entries (not just one) when triggered
- _build_consolidation_prompt accepts evicted entries and asks the
  LLM to integrate them into the existing long-term summary
- _update_long_term_memory and _aupdate_long_term_memory accept
  evicted entries parameter
- process_step and aprocess_step pass evicted entries to the
  consolidation methods
- Updated existing tests and added regression test verifying the
  LLM receives the evicted entries
@codecov
Copy link

codecov bot commented Mar 6, 2026

Codecov Report

❌ Patch coverage is 90.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.85%. Comparing base (4c0549e) to head (c8f7a47).
⚠️ Report is 19 commits behind head on main.

Files with missing lines Patch % Lines
mesa_llm/memory/st_lt_memory.py 90.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #166      +/-   ##
==========================================
+ Coverage   90.08%   90.85%   +0.76%     
==========================================
  Files          19       19              
  Lines        1503     1542      +39     
==========================================
+ Hits         1354     1401      +47     
+ Misses        149      141       -8     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Cover _aupdate_long_term_memory to ensure the async consolidation
path receives evicted entries and stores the LLM response correctly.
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 7, 2026

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: e389bcf2-02f5-49c7-8388-f0932cd159fa

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

STLTMemory loses popped memories instead of consolidating them into long-term memory

1 participant